package com.decoration.basic.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hutool.json.JSONUtil;
import com.decoration.basic.BasicController;
import com.decoration.basic.entity.*;
import com.decoration.basic.findCondition.DecCtctionFindCondition;
import com.decoration.basic.po.DecUserInfoPo;
import com.decoration.basic.service.HomePageService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.decoration.basic.service.DecCtctionService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.Global;

import java.util.List;

//@Slf4j
@Controller
@RequestMapping("/admin")
public class CtctionController extends BasicController {

	private static Logger log = LoggerFactory.getLogger(CtctionController.class);
	@Resource
	private DecCtctionService decCtctionService;
    @Resource
    private HomePageService homePageService;
	@Resource
	private DecUserService decUserService;

	//首页统计项
	//@PostMapping("/homePage/list")

	@RequestMapping(value = "/homePage/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult homePage(JsonResult jsonResult) {
		log.info("登录管理系统首页-获取展示数据开始!");

		//2.查询在建项目，总项目，今日提交日报数,今日已反馈项目数
		HomePageCount result = homePageService.findAll();

		//3.返回数据
		if (result == null){
			jsonResult.setCode(JsonResult.SUCCESS_CODE);
			jsonResult.setMsg("无统计数据");
			jsonResult.setCount(0);
			result = new HomePageCount();
			jsonResult.setData(result);
			return jsonResult;
		}

		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取数据成功");
		jsonResult.setCount(1);
		jsonResult.setData(result);
		return jsonResult;
	}


	// 管理系统 --模糊查找项目列表
//	@PostMapping("/ctction/list")
	@RequestMapping(value = "/ctction/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult ctclist(JsonResult jsonResult, @RequestBody DecCtctionFindCondition findCondition, HttpServletRequest request) {

		log.info("模糊查询项目列表开始");
		//1.校验用户权限为管理员
		String userid = (String)request.getSession().getAttribute(Global.SESSION_USER_ID);

		/*if (userid == null || userid.equals("")){  //测试专用
			userid = "1318fa54f304470ebffd7caf42573b23";
		}*/

		log.info("校验用户信息:["+userid+"]");
		DecorationUser user = decUserService.findById(userid);
		if (user ==null || user.getPerType() != 0){
			log.info("用户无权限获取项目列表信息"+ JSONUtil.parseObj(user));
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("用户无权限获取项目列表信息");
			return jsonResult;
		}
		//2.获取项目列表
		findCondition.setUserId(userid);
		setPagerInfo(findCondition);
		log.info("查询参数:["+JSONUtil.parseObj(findCondition)+"]");
		jsonResult = decCtctionService.findCtcList(findCondition);

		log.info("响应信息为：["+JSONUtil.toJsonStr(jsonResult)+"]");
		log.info("模糊查询项目列表结束");
		return jsonResult;
	}

	// 新增项目
	@PostMapping("/ctction/add")
	@ResponseBody
	public JsonResult ctcadd(JsonResult jsonResult, HttpServletRequest request, @RequestBody DecCtctionInMsg inf) {


		//校验用户信息
		String userid = (String) request.getSession().getAttribute(Global.SESSION_USER_ID);

		if (userid == null || userid.equals("")){
			userid = "1318fa54f304470ebffd7caf42573b23";//测试专用
		}

		DecorationUser user = decUserService.findById(userid);

		if (user ==null || inf == null){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("无此用户信息，请联系管理员");
			return jsonResult;
		}

		//校验必填项：
		//新增数据到数据库中
		Boolean result = decCtctionService.add(inf, user);
		if (!result) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("新增项目信息失败");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("新增项目信息成功");

		return jsonResult;
	}

	//获取业主，监理，设计师下拉信息
	@RequestMapping(value = "/userType/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getUserTypelist(JsonResult jsonResult,@RequestBody DecUserInfoPo userpo) {

		if (userpo.getPerType() == null || userpo.getPerType() == 0 ){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取用户下拉列表失败,请求参数异常");
			return jsonResult;
		}
		List<DecPhaseBase> list = decCtctionService.findUserTypelist(userpo.getPerType());
		if (list == null || list.size() == 0){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取用户下拉列表成功");
			jsonResult.setCount(0);
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取用户下拉列表成功");
		jsonResult.setCount(list.size());
		jsonResult.setData(list);
		return jsonResult;
	}

	// 用户修改项目内容
	@RequestMapping(value = "/ctction/modify", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult ctcmodify(JsonResult jsonResult, @RequestBody DecCtctionInMsg inMsg,HttpServletRequest request) {
        String userid = (String)request.getSession().getAttribute(Global.SESSION_USER_ID);

		if (userid == null || userid.equals("")){
			userid = "1318fa54f304470ebffd7caf42573b23";//测试专用
		}

		Boolean result = decCtctionService.update(inMsg,userid);
		if (!result) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("修改项目信息失败");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("修改项目信息成功");

        return jsonResult;
    }

    // 用户删除项目详情
    @RequestMapping(value ="/ctction/del", method = RequestMethod.POST)
	@ResponseBody
    public JsonResult detelectcbyid(JsonResult jsonResult,@RequestBody DecCtctionInMsg inMsg,HttpServletRequest request) {

//        String userid = (String)request.getSession().getAttribute(Global.SESSION_USER_ID);

        if (inMsg.getCtcid() == null){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目id为空，请确定项目信息");
			return jsonResult;
		}

        Boolean result = decCtctionService.delete(inMsg.getCtcid());

        if (!result){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("删除项目失败！");

			return jsonResult;
		}
        jsonResult.setCode(JsonResult.SUCCESS_CODE);
        jsonResult.setMsg("删除项目成功！");

        return jsonResult;
    }

	// 用户查找项目详情
	@PostMapping("/ctction/ctcid")
	@ResponseBody
	public JsonResult ctcbyid(JsonResult jsonResult,@RequestBody DecCtctionFindCondition findCondition,HttpServletRequest request) {

//		String userid = (String)request.getSession().getAttribute(Global.SESSION_USER_ID);

		DecCtctionOutMsg result = null;
		try {
			result = decCtctionService.searchCtcInfo(findCondition.getCtcid());
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(result ==null){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取项目详情信息失败");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取项目详情信息成功");
		jsonResult.setCount(1);
		jsonResult.setData(result);
		return jsonResult;
	}
}
